package com.dy.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.convert.WagesConvert;
import com.dy.dto.WagesAddDTO;
import com.dy.dto.WagesExcelDTO;
import com.dy.dto.WagesPageDTO;
import com.dy.dto.WagesUpdateDTO;
import com.dy.entity.WagesEntity;
import com.dy.mapper.WagesMapper;
import com.dy.service.WagesService;
import com.dy.vo.WagesVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: dy
 * @date: 2024/5/7 14:32
 * @ClassDescription:
 */
@Service
public class WagesServiceImpl extends ServiceImpl<WagesMapper, WagesEntity> implements WagesService {

    @Resource
    private WagesConvert wagesConvert;

    /**
     * 分页查询
     * @param dto dto
     * @return {@link IPage}<{@link WagesVO}>
     */
    @Override
    public IPage<WagesVO> pageList(WagesPageDTO dto) {
        IPage<WagesEntity> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<WagesEntity> page = this.page(iPage, Wrappers.<WagesEntity>lambdaQuery()
                .eq(StringUtils.isNotBlank(dto.getName()), WagesEntity::getName, dto.getName())
                .eq(WagesEntity::getUserId,StpUtil.getLoginId())
                .gt(ObjectUtil.isNotNull(dto.getRecordDateStart()), WagesEntity::getRecordDate, dto.getRecordDateStart())
                .lt(ObjectUtil.isNotNull(dto.getRecordDateEnd()), WagesEntity::getRecordDate, dto.getRecordDateEnd()));
        return page.convert(wagesConvert::wagesToVo);
    }
    /**
     * 新增
     * @param dto dto
     * @return {@link Boolean}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(WagesAddDTO dto) {
        WagesEntity entity = wagesConvert.wagesAddDtoToWagesEntity(dto);
        entity.setUserId(Convert.toLong(StpUtil.getLoginId()));
        return this.save(entity);
    }
    /**
     * 根据id查看
     * @param id id
     * @return {@link WagesVO}
     */
    @Override
    public WagesVO getWagesById(Long id) {
        WagesEntity wagesEntity = this.lambdaQuery()
                .eq(WagesEntity::getId, id)
                .eq(WagesEntity::getUserId, StpUtil.getLoginId())
                .one();
        return wagesConvert.wagesToVo(wagesEntity);
    }
    /**
     * 根据id删除
     * @param id id
     * @return {@link Boolean}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWagesById(Long id) {
        StpUtil.getLoginId();
        return this.removeById(id);
    }
    /**
     * 修改
     * @param dto dto
     * @return {@link Boolean}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateWages(WagesUpdateDTO dto) {
        StpUtil.getLoginId();
        WagesEntity entity = wagesConvert.wagesUpdateDtoToWagesEntity(dto);
        return this.updateById(entity);
    }

    /**
     * excel导出
     * @param dto dto
     * @return {@link WagesExcelDTO}
     */
    @Override
    public List<WagesExcelDTO> getList(WagesPageDTO dto) {
        List<WagesEntity> entityList = this.list(Wrappers.<WagesEntity>lambdaQuery()
                .eq(StringUtils.isNotBlank(dto.getName()), WagesEntity::getName, dto.getName())
                .eq(WagesEntity::getUserId, StpUtil.getLoginId())
                .gt(ObjectUtil.isNotNull(dto.getRecordDateStart()), WagesEntity::getRecordDate, dto.getRecordDateStart())
                .lt(ObjectUtil.isNotNull(dto.getRecordDateEnd()), WagesEntity::getRecordDate, dto.getRecordDateEnd()));
        return wagesConvert.wagesEntityListToWagesExcelDtoList(entityList);
    }
    /**
     * 工资导入
     * @param wagesExcelDtoList wagesExcelDtoList
     * @return {@link Boolean}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean excelImport(List<WagesExcelDTO> wagesExcelDtoList) {
        StpUtil.getLoginId();
        List<WagesEntity> wagesEntities = wagesConvert.wagesExcelDtoListToWagesEntityList(wagesExcelDtoList);
        List<WagesEntity> entityList = wagesEntities.stream()
                .peek(p -> p.setUserId(Convert.toLong(StpUtil.getLoginId())))
                .collect(Collectors.toList());
        return this.saveBatch(entityList);
    }
}
